package com.koron.standard.jobStock.repair.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelAnalysisStopException;
import com.koron.standard.deviceClasssify.service.TypeService;
import com.koron.standard.jobStock.excel.JobStockListener;
import com.koron.standard.jobStock.repair.bean.dto.RepairDTO;
import com.koron.standard.jobStock.repair.bean.dto.RepairItemDTO;
import com.koron.standard.jobStock.repair.bean.po.RepairItemParams;
import com.koron.standard.jobStock.repair.service.RepairService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jzp
 * @since 2022/2/23
 */
@Slf4j
public class RepairListener extends JobStockListener {

    private RepairService repairService;

    private RepairDTO repairDTO;
    private boolean isItem;

    public RepairListener(TypeService typeService, RepairService repairService) {
        super(typeService);
        this.repairService = repairService;
    }

    public String saveData() {
        if (repairDTO != null) {
            repairService.saveDto(repairDTO);
            return repairDTO.getId();
        }
        return null;
    }

    public void resetData() {
        isItem = false;
        repairDTO = new RepairDTO();
        repairDTO.setItemDTOs(new ArrayList<>());
    }

    @Override
    public String templateSheetName() {
        return "计划性检修标准模板";
    }

    @Override
    public void invoke(Map<Integer, String> map, AnalysisContext analysisContext) {
        if (isSkipped()) {
            return;
        }
        if ("作业项目".equals(map.get(0))) {
            isItem = true;
            return;
        }
        if ("作业标准名称".equals(map.get(0))) {
            // 作业标准名称
            if (StringUtils.isEmpty(map.get(2))) {
                setErrorMessage(getCurrentSheetName()+ "的作业标准名称不能为空");
                throw new ExcelAnalysisStopException();
            }
            repairDTO.setName(map.get(2));
            // 设备分类ID
            if (StringUtils.isEmpty(map.get(4))) {
                setErrorMessage(getCurrentSheetName()+ "的设备分类ID不能为空");
                throw new ExcelAnalysisStopException();
            }
            repairDTO.setTypeId(map.get(4));
            // 作业步骤层数
            repairDTO.setStepLevel(Integer.valueOf(map.get(6)));
            repairDTO.setAppid(getAppId());
            repairDTO.setStatus(0);
            repairDTO.setPublicStatus(0);
        }else if (isItem && map.get(1) != null) {
            // 没有.则在第一层
            if (!map.get(1).contains(".")) {
                // 加入最外层
                RepairItemDTO repairItemDTO = getByMap(map);
                if (repairDTO.getStepLevel() > 1) {
                    repairItemDTO.setChildren(new ArrayList<>());
                }
                repairDTO.getItemDTOs().add(repairItemDTO);
            } else {
                // 长度为3则在第二层
                List<RepairItemDTO> itemDTOs = repairDTO.getItemDTOs();
                // 获取最后一层
                RepairItemDTO itemDTO = itemDTOs.get(itemDTOs.size() - 1);
                if (CollectionUtils.isEmpty(itemDTO.getChildren())) {
                    itemDTO.setChildren(new ArrayList<>());
                }
                itemDTO.getChildren().add(getByMap(map));
            }
        }

    }

    private List<RepairItemParams> resolveItemParams(String str) {
        List<String[]> strings = resolveItemStr(str, "\n");
        return strings.stream().map(e -> {
            RepairItemParams repairItemParams = new RepairItemParams();
            repairItemParams.setName(e[0]);
            repairItemParams.setUnit(e[1]);
            return repairItemParams;
        }).collect(Collectors.toList());
    }

    private RepairItemDTO getByMap(Map<Integer, String> map) {
        RepairItemDTO repairItemDTO = new RepairItemDTO();
        repairItemDTO.setContext(map.get(2));
        repairItemDTO.setContextStandard(map.get(3));
        repairItemDTO.setRisk(map.get(4));
        repairItemDTO.setLevel(getLevel(map.get(6)));
        repairItemDTO.setItemParams(resolveItemParams(map.get(5)));
        return repairItemDTO;
    }

}
